Page Iteration In Progress

Index



Concept


Big O notation is used to describe the complexity of an algorithm.

Usually Big O assumes "worst-case". For example when doing a linear search through a list of N elements, it would assume that we would have to search to the end (N).

Big O is not a computation time measurement.
Big O tells us how fast computation time grows relative to the growth of N (data size).

Side Note: The "O" is thought to be derived from mathematical words beginning with "O" e.g. German "Ordnung von" for "Order of".

O(1)


O(1) means performance is not dependent on data set size.
For example "get" would on an array is typically O(1)



O(N)

O(N) means performance is linearly dependent on the data set size.

For example, if iterating through a collection searching for an element, we would have O(N) performance.

O(N2)


O(N2) means performance is proportional to the square of the size of the data set.

This performance occurs for nested data.

O(log N)


An example of O(log N) is a binary search.

Also see comparison...

Big O Wasted Space Analysis


Note the "Wasted space (average)" row here on wiki.

When talking about wasted space we are talking about any space that is not the actual data. The table shows us that many structures have a wasted space Big O of O(N). One exception is that a fixed size array which has a Big O of O(0), which means no wasted space.

What is "wasted space"
Linked lists and even dynamic arrays have references (pointers), to the data, which is considered "wasted space". Therefore, they have a Big O of (N).

Insignificant wasted space
However, before we run off and make everything a fixed size array, we need to make an estimate of "how much" wasted space and determine if it is significant. For a dynamic array, singly linked list, and doubly linked list we're talking about something like 4-12 bytes of "wasted space" for the references (pointers). This would often be small compared to the size of one object. Just one string field might consume many more bytes than 12.

Significant wasted space
Where "wasted space" would be more significant would be where the data elements are very small. One example might be "Point" objects (with an int x and an int y field).

Linked List Versus Array


We can use Big O to help us choose a data structure. For example let's say we are choosing between an array type structure versus a linked list.

Here is a good comparison between these two structures: wiki.

From the referenced table we can conclude:

Conclusions simplified:

Comparison


Remember Big-O is not a time (or space) measurement but an indicator of growth as a function of increasing data size.

N is the data set size. The displayed values are indicators of "growth" (not time). In other words, use this chart as a general indicator.

Here is a general growth comparison table:

NO(1)O(N)O(log N)
101101
10011002
1,00011,0003
10,000110,0004
100,0001100,0005
1,000,00011,000,0006
10,000,000110,000,0007
100,000,0001100,000,0008
1,000,000,00011,000,000,0009
10,000,000,000110,000,000,00010


References